<?php

namespace Common\Model;

use Common\Model\AiModel;


class DoctorModel extends AiModel
{

  function VerPhoneRegister($phone){
    $where['phone']=$phone;
    $info=$this->where($where)->find();
      if($info){//如果发现此手机号码
        return 1;
      }else{ //如果没有发现此手机号
        return 0;
      }
    }

    //添加新用户
    function addDoctor($phone,$password){
    	$whereAdd['phone']   =$phone;
    	$whereAdd['ctime']   =time();
    	$whereAdd['password']=$password;
      $whereAdd['name']    =defaultName();
      $whereAdd['head']    =defaultHead();
      $whereAdd['sex']     ='n';
      $whereAdd['token']   =creatToken();
      $info=$this->add($whereAdd);
      if($info){
       return $info;
     }else{
       return 0;
     }
   }
     //手机号码 密码验证 查找用户
  function findone($phone,$password){
    $where['password']=$password;
    $where['phone']   =$phone;
    $info=$this->where($where)->field('id')->find();

    if($info){
     $data=$this->toIdGetDoctorInfo($info['id'],$field='id,name,college,head,type,star,sex,hospital_id,phone,birthday,token,pro,city,area,shanchang,ctime','login');
     return $data;
   }else{
     return 0;
   }
 }
  


    //通过id获取用户信息

   function toIdGetDoctorInfo($id,$field='id,name,college,head,type,star,sex,hospital_id,phone,birthday,token,pro,city,area,shanchang,ctime,answer,feedback,introduction',$type){
    $this->upToken($id);
    $where['id']=$id;
    $field=$field?$field:'*';
    $info= $this->where($where)->field($field)->find();
    //获取医生的所在医院
    $info['hos_name']     =D('Hospital')->ToIdGetHospitalInfo($info['hospital_id'])['hos_name'];
    //医生的类型名字
    $info['typename']     =D('DoctorType')->ToIdGetName($info['type'])['dt_name'];
    //医生擅长
    $info['shanchangfont']=D('DoctorClass')->ToIdStringGetFont($info['shanchang']);
    $info['shanchang']    =D('DoctorClass')->ToIdStringGetArrayFont($info['shanchang']);
    //医生头像
    $info['head']         =C('HOST_URL').$info['head'];
    $areastring           =$this->toDidGetAreaId($id);
    $info['address']      =D('Area')->toUidGetAddressFont($areastring);
    //医生身份的审核状态
    $info['provestatus']  =D('Prove')->VerDoctorProve($id);
    //创建时间
    $info['ctime']  =date('Y-m-d',$info['ctime']);

    if($type=='login'||$type=='register'){
         $info['sig']=A('Im')->toUidCreatSig($id);

    }
    return $info;
  }
  

      //通过用户id 获取 area id string
 function toDidGetAreaId($uid){
    $where['id']=$uid;
    $uInfo=$this->where($where)->find();
    return $uInfo['pro'].'-'.$uInfo['city'].'-'.$uInfo['area'];
 }

 
 


    //修改token
 function upToken($id){
   $where['id']=$id;
   $token=creatToken();
   $save['token']=$token;
   $saveinfo=$this->where($where)->save($save);
   if($saveinfo){
     return $token;
   }else{
     return 0;
   }

 } 
     //通过手机号码更新数据
 function UpToPhone($phone,$save){
   $where['phone']=$phone;
   $info=$this->where($where)->save($save);
   if($info!==false){
     return 1;
   }else{
     return 0;
   }
 }
   //通过ID更新数据
 function UpToId($id,$save){
   $where['id']=$id;
   $info=$this->where($where)->save($save);
   if($info!==false){
     return 1;
   }else{
     return 0;
   }
 }


 function getList($page=0,$num=5){
  $info=$this->field('id')->field('id')->limit($page*5,$num)->select();

  $doctorinfo=array();
  foreach ($info as $key => $value) {
   $doctorinfo[$key]=$this->toIdGetDoctorInfo($value['id']);
  }
  if($doctorinfo){
   return $doctorinfo;
 }else{
   return 0;
 }
}


//获取医生的属性
function getAttr($did){
   
}
//通过医院id获取医生的信息
function toHostIdGetDoctorInfo($hid){
   $array=array();
   $where['hospital_id']=$hid;
   $dinfo=$this->where($where)->select();
   foreach ($dinfo as $key => $value) {
     $array[]=$this->toIdGetDoctorInfo($value['id']);
   }
   return $array;
}


}
